home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Unix / Shells / zsh / Source / src / zle_vi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-07  |  5.8 KB  |  372 lines

  1.  
  2. /*
  3.  *
  4.  * zle_vi.c - vi-specific functions
  5.  *
  6.  * This file is part of zsh, the Z shell.
  7.  *
  8.  * This software is Copyright 1992 by Paul Falstad
  9.  *
  10.  * Permission is hereby granted to copy, reproduce, redistribute or otherwise
  11.  * use this software as long as: there is no monetary profit gained
  12.  * specifically from the use or reproduction of this software, it is not
  13.  * sold, rented, traded or otherwise marketed, and this copyright notice is
  14.  * included prominently in any copy made.
  15.  *
  16.  * The author make no claims as to the fitness or correctness of this software
  17.  * for any use whatsoever, and it is provided as is. Any use of this software
  18.  * is at the user's own risk.
  19.  *
  20.  */
  21.  
  22. #define ZLE
  23. #include "zsh.h"
  24.  
  25. SPROTO(void startvichange, (int im));
  26.  
  27. static void startvichange(im)
  28. int im;
  29. {
  30.     insmode = im;
  31.     if (vichgbuf)
  32.     free(vichgbuf);
  33.     vichgbuf = (char *)zalloc(vichgbufsz = 16);
  34.     vichgbuf[0] = c;
  35.     vichgbufptr = 1;
  36.     vichgflag = 1;
  37.     viinsbegin = cs;
  38. }
  39.  
  40. SPROTO(void startvitext, (int im));
  41.  
  42. static void startvitext(im)
  43. int im;
  44. {
  45.     startvichange(im);
  46.     bindtab = mainbindtab;
  47.     undoing = 0;
  48. }
  49.  
  50. int vigetkey()
  51. {                /**/
  52.     int cmd;
  53.  
  54.     if ((cmd = getkeycmd()) < 0 || cmd == z_sendbreak) {
  55.     feep();
  56.     return 0;
  57.     }
  58.     if (cmd == z_quotedinsert) {
  59.     if ((c = getkey(0)) == EOF)
  60.         return 0;
  61.     return c;
  62.     } else if (cmd == z_vicmdmode)
  63.     return 0;
  64.     return c;
  65. }
  66.  
  67. int getvirange(wf)        /**/
  68. int wf;
  69. {
  70.     int k2, t0, startline, endline;
  71.  
  72.     startline = findbol();
  73.     endline = findeol();
  74.     for (;;) {
  75.     
  76.     if ((k2 = getkeycmd()) < 0 || k2 == z_sendbreak) {
  77.         feep();
  78.         return -1;
  79.     }
  80.     if (zlecmds[k2].flags & ZLE_ARG)
  81.         (*zlecmds[k2].func) ();
  82.     else
  83.         break;
  84.     }
  85.     if (k2 == bindk) {
  86.     findline(&cs, &t0);
  87.     return (t0 == ll) ? t0 : t0 + 1;
  88.     }
  89.     if (!(zlecmds[k2].flags & ZLE_MOVEMENT)) {
  90.     feep();
  91.     return -1;
  92.     }
  93.     t0 = cs;
  94.  
  95.     virangeflag = 1;
  96.     wordflag = wf;
  97.     (*zlecmds[k2].func) ();
  98.     wordflag = virangeflag = 0;
  99.     if (cs == t0) {
  100.     feep();
  101.     return -1;
  102.     }
  103.     if (startline != findbol()) {
  104.     if (zlecmds[k2].flags & ZLE_LINEMOVE) {
  105.         if (cs < t0) {
  106.         cs = startline;
  107.         t0 = findeol() + 1;
  108.         } else {
  109.         t0 = startline;
  110.         cs = findeol() + 1;
  111.         }
  112.     } else {
  113.         if (cs < startline)
  114.         cs = startline;
  115.         else if (cs >= endline)
  116.         cs = endline - 1;
  117.     }
  118.     }
  119.     if (cs > t0) {
  120.     k2 = cs;
  121.     cs = t0;
  122.     t0 = k2;
  123.     }
  124.     return t0;
  125. }
  126.  
  127. void viaddnext()
  128. {                /**/
  129.     if (cs != ll)
  130.     cs++;
  131.     startvitext(1);
  132. }
  133.  
  134. void viaddeol()
  135. {                /**/
  136.     cs = findeol();
  137.     startvitext(1);
  138. }
  139.  
  140. void viinsert()
  141. {                /**/
  142.     startvitext(1);
  143. }
  144.  
  145. void viinsertbol()
  146. {                /**/
  147.     cs = findbol();
  148.     startvitext(1);
  149. }
  150.  
  151. void videlete()
  152. {                /**/
  153.     int c2;
  154.  
  155.     startvichange(1);
  156.     if ((c2 = getvirange(0)) == -1) {
  157.     vichgflag = 0;
  158.     return;
  159.     }
  160.     forekill(c2 - cs, 0);
  161.     vichgflag = 0;
  162. }
  163.  
  164. void videletechar()
  165. {                /**/
  166.     if (mult < 0) {
  167.     mult = -mult;
  168.     vibackwarddeletechar();
  169.     return;
  170.     }
  171.     if (!(cs + mult > ll || line[cs] == '\n')) {
  172.     if (vichgbuf == NULL)
  173.         vichgbuf = (char *)zalloc(vichgbufsz = 16);
  174.     vichgbufptr = 1;
  175.     vichgbuf[0] = c;
  176.     cs += mult;
  177.     backkill(mult, 0);
  178.     if (cs && (cs == ll || line[cs] == '\n'))
  179.         cs--;
  180.     } else
  181.     feep();
  182. }
  183.  
  184. void vichange()
  185. {                /**/
  186.     int c2;
  187.  
  188.     startvichange(1);
  189.     if ((c2 = getvirange(1)) == -1) {
  190.     vichgflag = 0;
  191.     return;
  192.     }
  193.     forekill(c2 - cs, 0);
  194.     bindtab = mainbindtab;
  195.     undoing = 0;
  196. }
  197.  
  198. void visubstitute()
  199. {                /**/
  200.     if (mult < 0)
  201.     return;
  202.     if (findeol() - cs < mult)
  203.     mult = findeol() - cs;
  204.     if (mult) {
  205.     foredel(mult);
  206.     startvitext(1);
  207.     }
  208. }
  209.  
  210. void vichangeeol()
  211. {                /**/
  212.     killline();
  213.     startvitext(1);
  214. }
  215.  
  216. void vichangewholeline()
  217. {                /**/
  218.     int cq;
  219.  
  220.     findline(&cs, &cq);
  221.     foredel(cq - cs);
  222.     startvitext(1);
  223. }
  224.  
  225. void viyank()
  226. {                /**/
  227.     int c2;
  228.  
  229.     if ((c2 = getvirange(0)) == -1)
  230.     return;
  231.     cut(cs, c2 - cs, 0);
  232. }
  233.  
  234. void viyankeol()
  235. {                /**/
  236.     int x = findeol();
  237.  
  238.     if (x == cs)
  239.     feep();
  240.     else
  241.     cut(cs, x - cs, 0);
  242. }
  243.  
  244. void vireplace()
  245. {                /**/
  246.     startvitext(0);
  247. }
  248.  
  249. void vireplacechars()
  250. {                /**/
  251.     int ch;
  252.  
  253.     if (mult < 0)
  254.     return;
  255.     if (mult + cs > ll) {
  256.     feep();
  257.     return;
  258.     }
  259.     startvichange(1);
  260.     if ((ch = vigetkey()))
  261.     while (mult--)
  262.         line[cs++] = ch;
  263.     vichgflag = 0;
  264.     cs--;
  265. }
  266.  
  267. void vicmdmode()
  268. {                /**/
  269.     if (bindtab == altbindtab)
  270.     feep();
  271.     else {
  272.     bindtab = altbindtab;
  273.     if (cs)
  274.         cs--;
  275.     undoing = 1;
  276.     if (vichgflag)
  277.         vichgflag = 0;
  278.     }
  279. }
  280.  
  281. void viopenlinebelow()
  282. {                /**/
  283.     cs = findeol();
  284.     spaceinline(1);
  285.     line[cs++] = '\n';
  286.     startvitext(1);
  287. }
  288.  
  289. void viopenlineabove()
  290. {                /**/
  291.     cs = findbol();
  292.     spaceinline(1);
  293.     line[cs] = '\n';
  294.     startvitext(1);
  295. }
  296.  
  297. void vioperswapcase()
  298. {                /**/
  299.     int c2;
  300.  
  301.     if ((c2 = getvirange(0)) == -1)
  302.     return;
  303.     while (cs < c2) {
  304.     int ch = line[cs];
  305.  
  306.     if (islower(ch))
  307.         ch = tuupper(ch);
  308.     else if (isupper(ch))
  309.         ch = tulower(ch);
  310.     line[cs++] = ch;
  311.     }
  312. }
  313.  
  314. void virepeatchange()
  315. {                /**/
  316.     if (!vichgbuf || bindtab == mainbindtab || vichgflag)
  317.     feep();
  318.     else
  319.     ungetkeys(vichgbuf, vichgbufptr);
  320. }
  321.  
  322. void viindent()
  323. {                /**/
  324.     int c2, endcs, t0, rmult;
  325.  
  326.     if (mult < 0) {
  327.     mult = -mult;
  328.     viunindent();
  329.     return;
  330.     }
  331.     rmult = mult;
  332.     if ((c2 = getvirange(0)) == -1)
  333.     return;
  334.     if (cs != findbol()) {
  335.     feep();
  336.     return;
  337.     }
  338.     endcs = cs + rmult;
  339.     while (cs < c2) {
  340.     spaceinline(rmult);
  341.     for (t0 = 0; t0 != rmult; t0++)
  342.         line[cs++] = '\t';
  343.     cs = findeol() + 1;
  344.     }
  345.     cs = endcs;
  346. }
  347.  
  348. void viunindent()
  349. {                /**/
  350.     int c2, endcs, t0, rmult;
  351.  
  352.     rmult = mult;
  353.     if (mult < 0) {
  354.     mult = -mult;
  355.     viindent();
  356.     return;
  357.     }
  358.     if ((c2 = getvirange(0)) == -1)
  359.     return;
  360.     if (cs != findbol()) {
  361.     feep();
  362.     return;
  363.     }
  364.     endcs = cs;
  365.     while (cs < c2) {
  366.     for (t0 = 0; t0 != rmult && line[cs] == '\t'; t0++)
  367.         foredel(1);
  368.     cs = findeol() + 1;
  369.     }
  370.     cs = endcs;
  371. }
  372.